//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//

// Import the interfaces
#import "NumberGameScene.h"
#import "MainScene.h"
#import "NumberAnswerScene.h"
#import "SimpleAudioEngine.h"

extern int nNumberLevel;
int nNumberNumber = -1;
int nNumberValue = -1;

extern BOOL isSound;
extern BOOL isNormal;

// HelloWorld implementation
@implementation NumberGameScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	NumberGameScene *layer = [NumberGameScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		nCount = 0;
		numberCount = 0;
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		// ask director the the window size
		CGSize size = [[Director sharedDirector] winSize];
		
		Sprite *sprite = [Sprite spriteWithFile:@"einstein.png"];
		sprite.position = ccp( 0, 50);
		[self addChild:sprite];		
		id rotateAction = [RotateBy actionWithDuration:8 angle:180*4];		
		id jumpAction = [JumpBy actionWithDuration:8 position:ccp(size.width,0) height:200 jumps:4];		
		id fordward = [Spawn actions:rotateAction, jumpAction, nil];		
		id backwards = [fordward reverse];
		id sequence = [Sequence actions: fordward, backwards, nil];
		id repeat = [RepeatForever actionWithAction:sequence];
		[sprite runAction:repeat];
		
		BitmapFontAtlas *labelTitle = [BitmapFontAtlas bitmapFontAtlasWithString:@"Back" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *itemBack = [MenuItemLabel itemWithLabel:labelTitle target:self selector:@selector(BackToMain:)];
		Menu *menu = [Menu menuWithItems:itemBack, nil];
		menu.position = ccp(420, 30);
		[self addChild:menu];
		
		BitmapFontAtlas *label = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
		[self addChild:label z:0 tag:1];
		label.position = ccp(240, 140);
		
		BitmapFontAtlas *label1 = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
		[self addChild:label1 z:0 tag:2];
		label1.position = ccp(240, 180);
		
		NSString *string = [NSString stringWithFormat:@"Level %d", nNumberLevel];
		BitmapFontAtlas *labelLevel = [BitmapFontAtlas bitmapFontAtlasWithString:string fntFile:@"bitmapFontTest.fnt"];
		[self addChild:labelLevel z:0 tag:1];
		labelLevel.position = ccp(80, 280);
		
		float td;
		if (isNormal == YES) {
			td = 2.0f;
		}
		else {
			td = 1.0f;
		}

		[self schedule:@selector(step:) interval: td];
	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

- (void) BackToMain: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[MainScene node]]];
}

-(void) step:(ccTime) dt
{
	nCount++;
	if (nCount == 1) {
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:2];
		[label1 setString:@"Ready"];
//		nNumber = (int)(CCRANDOM_0_1() * (nLevel + 4)) + 1;
		nNumberNumber = 1 + arc4random() % (nNumberLevel + 4);
		if (isSound == YES) {
			[[SimpleAudioEngine sharedEngine] playEffect:@"ready.wav"];		
		}
	}
	if (nCount == 2) {
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:2];
		[label1 setString:@"Go"];
		//		nNumber = (int)(CCRANDOM_0_1() * (nLevel + 4)) + 1;
		nNumberNumber = 1 + arc4random() % (nNumberLevel + 4);
		if (isSound == YES) {
			[[SimpleAudioEngine sharedEngine] playEffect:@"go.wav"];
		}
	}
	if (nCount > 2 && nCount < nNumberLevel + 7) {
		numberCount++;
//		int number = (int)(CCRANDOM_0_1() * 10);
		int number = arc4random() % 10;
		NSString *string = [NSString stringWithFormat:@"%d", number];
		BitmapFontAtlas *label = (BitmapFontAtlas*) [self getChildByTag:1];
		[label setString:string];
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:2];
		NSString *string1 = [NSString stringWithFormat:@"No.%d", numberCount];
		[label1 setString:string1];
		if (numberCount == nNumberNumber) {
			nNumberValue = number;
		}
		
		if (isSound == YES) {
			switch (number) {
				case 0:
					[[SimpleAudioEngine sharedEngine] playEffect:@"0.wav"];
					break;
				case 1:
					[[SimpleAudioEngine sharedEngine] playEffect:@"1.wav"];
					break;
				case 2:
					[[SimpleAudioEngine sharedEngine] playEffect:@"2.wav"];
					break;
				case 3:
					[[SimpleAudioEngine sharedEngine] playEffect:@"3.wav"];
					break;
				case 4:
					[[SimpleAudioEngine sharedEngine] playEffect:@"4.wav"];
					break;
				case 5:
					[[SimpleAudioEngine sharedEngine] playEffect:@"5.wav"];
					break;
				case 6:
					[[SimpleAudioEngine sharedEngine] playEffect:@"6.wav"];
					break;
				case 7:
					[[SimpleAudioEngine sharedEngine] playEffect:@"7.wav"];
					break;
				case 8:
					[[SimpleAudioEngine sharedEngine] playEffect:@"8.wav"];
					break;
				case 9:
					[[SimpleAudioEngine sharedEngine] playEffect:@"9.wav"];
					break;
				default:
					break;
			}
		}
	}
	if (nCount == nNumberLevel + 7) {
		BitmapFontAtlas *label = (BitmapFontAtlas*) [self getChildByTag:1];
		[label setString:@""];
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:2];
		[label1 setString:@""];
		[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[NumberAnswerScene node]]];
	}

}
@end
